﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Control;
using HiStaff.Domain.PA;
using HiStaff.Domain.Sys;
using HiStaff.Util;
using HiStaff.Util.Enum;
using HiStaff.Base;
using HiStaff.Domain.LST;
using Janus.Windows.UI.CommandBars;
using HiStaff.iSystem;
using Janus.Windows.EditControls;

namespace HiStaff.iPay
{
    public partial class BasePaySheet : FormBase
    {
        public PaySheet MngPaySheet { get; set; }

        #region private vars
        private List<LST_SALPERIOD> _lstOrgPeriod;
        private bool bindingPeriod = false;
        private const string TCMD_YEAR = "TCMD_YEAR";
        private const string DDCMD_TIMEPERIOD = "DDCMD_TIMEPERIOD";
        private const string DDCMD_SALGROUP = "DDCMD_SALGROUP";

        private TextBox txtYear
        {
            get { return base.GetCommandByKey(TCMD_YEAR).GetTextBox(); }
        }
        private UIComboBox cboPeriod
        {
            get { return base.GetCommandByKey(DDCMD_TIMEPERIOD).GetUIComboBox(); }
        }
        private UIComboBox cboSalGroup
        {
            get { return base.GetCommandByKey(DDCMD_SALGROUP).GetUIComboBox(); }
        }
        #endregion

        public BasePaySheet()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            try
            {
                VisibleProgressBar = true;
                VisibleStatusTextBar = true;
                SetStatusText(string.Empty, MessageIcon.Null);

                base.CreateDropdownInCommandBarWithDataList(DDCMD_SALGROUP, "Nhóm lương", 100, "SALGROUP");

                base.CreateTxtInCommandBar(TCMD_YEAR, "Năm", 40, DateTime.Now.Year.ToString());

                base.CreateDropdownInCommandBar(DDCMD_TIMEPERIOD, "Kỳ lương", 80, 12);

                base.CreateCommandBar(CommandBarManager.ActionCommand.Calculate,
                                        CommandBarManager.ActionCommand.Separator,
                                        CommandBarManager.ActionCommand.Lock,
                                        CommandBarManager.ActionCommand.UnLock,
                                        CommandBarManager.ActionCommand.Separator,
                                        CommandBarManager.ActionCommand.SLock,
                                        CommandBarManager.ActionCommand.Separator,
                                        CommandBarManager.ActionCommand.Exit);
                LoadTimePeriodByOrg();
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }

        private void LoadTimePeriodByOrg()
        {
            try
            {
                bindingPeriod = true;
                decimal year;
                if (!(decimal.TryParse(txtYear.Text, out year) && txtYear.Text.Length == 4))
                {
                    return;
                }

                int periodSelected = 0;
                UICommand command = base.GetCommandByKey(DDCMD_TIMEPERIOD);
                bool isSearch = false;

                if (command != null)
                {
                    if (command.GetUIComboBox().SelectedIndex >= 0)
                        periodSelected = ((LST_SALPERIOD)command.GetUIComboBox().SelectedItem.DataRow).ENDDATE.Value.Month;
                }

                _lstOrgPeriod = Service.PayrollClient.GetActiveSalaryPeriod(year, trvOrg.OrgId);
                base.SetDataSourceToDropdownCommand(DDCMD_TIMEPERIOD, _lstOrgPeriod, "NAME", "ID", true);

                if (_lstOrgPeriod.Count > 0)
                {
                    if (command != null)
                    {
                        command.GetUIComboBox().SelectedIndex = 0;
                        if (periodSelected == 0)
                            periodSelected = DateTime.Now.Month;

                        for (int i = 0; i < _lstOrgPeriod.Count - 1; i++)
                        {
                            if (_lstOrgPeriod[i].ENDDATE.Value.Month >= periodSelected && _lstOrgPeriod[i].PASTATUS == (decimal?)PayrollStatus.Working)
                            {
                                command.GetUIComboBox().SelectedIndex = i;
                                isSearch = true;
                                break;
                            }
                        }
                        if (!isSearch)
                        {
                            for (int i = _lstOrgPeriod.Count - 1; i >= 0; i--)
                            {
                                if (_lstOrgPeriod[i].ENDDATE.Value.Month < periodSelected && _lstOrgPeriod[i].COSTATUS == (decimal?)ColexStatus.Working)
                                {
                                    command.GetUIComboBox().SelectedIndex = i;
                                    break;
                                }
                            }
                        }
                    }
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
            finally
            {
                LoadEmployeeByPeriod();
                bindingPeriod = false;
            }
        }

        private void LoadEmployeeByPeriod()
        {
            grdEmployee.DataSource = Service.PayrollClient.GetEmployeeByPeriod(new PA_SUM_SEARCH
            {
                PERIOD = ConvertHelper.ToDecimal(cboPeriod.SelectedValue),
                ORGID = trvOrg.OrgId,
                SALGROUP = ConvertHelper.ToDecimal(cboSalGroup.SelectedValue),
                TERMINATE = MngPaySheet == PaySheet.FinalPayment ? 1 : 0
            });
            grdEmployee.Refresh();
        }

        protected override bool CommandValueChanged(UICommand Command)
        {
            switch (Command.Key)
            {
                case TCMD_YEAR:
                    LoadTimePeriodByOrg();
                    break;
                default:
                    if (!bindingPeriod)
                        LoadEmployeeByPeriod();
                    break;
            }
            return true;
        }

        private void trvOrg_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                if (IsLoaded)
                {
                    LoadTimePeriodByOrg();
                }
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
            }
        }

    }
}
